﻿using System;
using System.Globalization;
using System.Windows.Forms;

namespace WorkLogger.Client.Forms {
	public partial class WorkedTimeReportForm : Form, IWorkedTimeReportForm {
		public WorkedTimeReportForm() {
			InitializeComponent();
		}

		public bool DeductIncomeTax {
			get { return deductIncomeTaxCheckBox.Checked; }
			set { deductIncomeTaxCheckBox.Checked = value; }
		}

		public event EventHandler BuildReport;

		public void ShowReport(WorkedTimeReport report) {
			listView.Items.Clear();
			foreach (WorkedTimeReport.WorkDayReportItem reportItem in report.WorkedDays) {
				var listViewItem = new ListViewItem(reportItem.WorkDate.ToString("d", new CultureInfo("ru-Ru")));
				listViewItem.SubItems.Add(TimeSpanFormatHelper.FormatTime(reportItem.StartWork));
				listViewItem.SubItems.Add(TimeSpanFormatHelper.FormatDuration(reportItem.LunchTime));
				listViewItem.SubItems.Add(TimeSpanFormatHelper.FormatTime(reportItem.EndWork));
				listViewItem.SubItems.Add(TimeSpanFormatHelper.FormatDuration(reportItem.WorkedTime));
				listViewItem.SubItems.Add(FormatMoney(reportItem.Income));
				listView.Items.Add(listViewItem);
			}
			var totalListViewItem = new ListViewItem("Итог");
			totalListViewItem.SubItems.Add("");
			totalListViewItem.SubItems.Add("");
			totalListViewItem.SubItems.Add("");
			totalListViewItem.SubItems.Add(TimeSpanFormatHelper.FormatDuration(report.TotalWorkedTime));
			totalListViewItem.SubItems.Add(FormatMoney(report.TotalIncome));
			listView.Items.Add(totalListViewItem);
		}

		private static string FormatMoney(decimal money) {
			return string.Format("{0} руб.", money.ToString("0,0.00", CultureInfo.InvariantCulture));
		}

		public DateTime BeginPeriod {
			get { return beginPeriodDateTimePicker.Value; }
			set { beginPeriodDateTimePicker.Value = value; }
		}

		public DateTime EndPeriod {
			get { return endPeriodDateTimePicker.Value; }
			set { endPeriodDateTimePicker.Value = value; }
		}

		private decimal salary;

		public decimal Salary {
			get { return salary; }
			set {
				salary = value;
				salaryTextBox.Text = salary.ToString(CultureInfo.InvariantCulture);
			}
		}

		private void BuildReportToolStripMenuItemClick(object sender, EventArgs e) {
			if (BuildReport != null) {
				BuildReport(this, e);
			}
		}
	}
}